
<!-- 设备周期设置 -->
<template>
    <div class="page">
        <div class="nuclein-query">
            <div>设备周期设置</div>
            <div style="margin-left: auto;">
                <el-button size="mini" type="primary" @click="handlerAdd()">添加</el-button>
            </div>
        </div>
        <div class="content-row">
            <el-table :data="tableData" style="width: 100%" border>
                <el-table-column label="设备购入的时间" prop="deviceBuyTime" align="center">
                </el-table-column>
                <el-table-column label="设备使用期限" align="center">
                    <template slot-scope="scope">
                        <el-tag type="success">
                            {{ scope.row.deviceDueTime + '年' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="设备id" prop="deviceIds" align="center">
                </el-table-column>
                <el-table-column label="设备类型" align="center">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.deviceType == 0 ? 'success':'primary'">
                            {{ scope.row.deviceType == 0 ? '门禁机' : '摄像头' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="设备到期的天数" align="center">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.overdueDay != null ? 'danger' : 'success'">
                            {{ scope.row.overdueDay == null ? '0天' : scope.row.overdueDay + '天' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="设备到期的状态" prop="diagnosisTime" align="center">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.cycleState? 'danger' : 'success'">
                            {{ scope.row.cycleState? '已到期' : '未到期' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="160">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger"
                            @click="handleDelete(scope.$index, scope.row.cycleId)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                :page-sizes="[10, 20, 30, 40]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
        <div>
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="form">
                    <el-form-item label="设备购入的时间" :label-width="formLabelWidth">
                        <el-date-picker v-model="form.deviceBuyTime" type="datetime" placeholder="选择设备购入的时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="设备到期时间设定" :label-width="formLabelWidth">
                        <el-input placeholder="请输入设备到期时间设定" v-model="form.deviceDueTime">
                            <template slot="append">年</template>
                        </el-input>
                    </el-form-item>
                    <!-- <div v-if="title != '编辑'"> -->
                        <el-form-item label="设备id" :label-width="formLabelWidth">
                            <el-select v-model="form.deviceId" placeholder="请选择设备id">
                                <el-option v-for="item in deviceIdList" :key="item.deviceId" :label="item.deviceName"
                                    :value="item.deviceId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="设备类型" :label-width="formLabelWidth">
                            <el-select v-model="form.deviceType" placeholder="请选择设备类型">
                                <el-option label="门禁机" value="0"></el-option>
                                <el-option label="摄像头" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    <!-- </div> -->
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="Submite">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { cycle, addcycle, getEquipment, delCycle } from '../../api/api'
import commonFunction from '../../utils/commonFunction'
export default {

  data () {
    return {
      dialogFormVisible: false,
      form: {
        deviceBuyTime: '', // 设备购入的时间
        deviceDueTime: 1, // 设备到期时间设定 年为单位  1 2 3
        deviceType: '',
        deviceId: ''
      },
      title: '添加',
      total: 10,
      page: 1,
      limit: 10,
      formLabelWidth: '140px',
      tableData: [],
      deviceIdList: []
    }
  },
  created () {
    this.getDeviceId()
    setTimeout(() => {
      this.gitList()
    }, 1);
  },
  methods: {
    // 获取
    gitList () {
      const data = {
        page: this.page,
        limit: this.limit
      }
      cycle({ ...data }).then(res => {
        if (res.code == 0) {
          const { total, records } = res.page
          this.tableData = records.map(item => {
            const deviceBuyTime = commonFunction.getTimeFormat(+new Date(item.deviceBuyTime), 'YYYY-MM-DD hh:mm:ss')
            const updateDate = commonFunction.getTimeFormat(+new Date(item.updateDate), 'YYYY-MM-DD hh:mm:ss')
            let deviceIds
            this.deviceIdList.forEach(it => {
              if (item.deviceId == it.deviceId) {
                deviceIds = it.deviceName
              }
            })
            return {
              ...item,
              deviceIds,
              updateDate,
              deviceBuyTime
            }
          })
          this.total = total
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 获取设备id
    getDeviceId () {
      getEquipment().then(res => {
        if (res.code === 0) {
          const { list } = res
          this.deviceIdList = list
        }
      })
    },
    // 提交
    Submite () {
      const data = {
        cycleId: this.form.cycleId ? this.form.cycleId : undefined,
        deviceBuyTime: this.form.deviceBuyTime, // 设备购入的时间
        deviceDueTime: 1, // 设备到期时间设定 年为单位  1 2 3
        deviceId: this.form.deviceId, // 设备id
        deviceType: this.form.deviceType
      }
      addcycle(data).then(res => {
        if (res.code == 0) {
          this.form = {
            deviceBuyTime: '',
            deviceDueTime: 1
          }
          this.$message.success(data.cycleId != null ? '编辑成功' : '添加成功')
          this.gitList()
          this.dialogFormVisible = false
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handlerAdd () {
      this.title = '添加'
      this.form = {
        cycleId: null,
        deviceBuyTime: '',
        deviceDueTime: 1,
        deviceId: '',
        deviceType: ''
      }
      this.dialogFormVisible = true
    },
    // 编辑
    handleEdit (index, row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      this.form = {
        cycleId: row.cycleId,
        deviceBuyTime: row.deviceBuyTime,
        deviceDueTime: row.deviceDueTime,
        deviceId: row.deviceId + '',
        deviceType: row.deviceType + ''
      }
    },
    handleDelete (index, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          cycleId: row
        }
        delCycle({ ...data }).then(res => {
          if (res.code == 0) {
            this.$message.success('删除成功')
            this.gitList()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分页
    handleSizeChange (val) {
      this.limit = val
      this.gitList()
    },
    handleCurrentChange (val) {
      this.page = val
      this.gitList()
    }
  }
}
</script>

<style scoped>
.page {
    position: relative;
    height: 100%;
}

.nuclein-query {
    display: flex;
    align-items: center;
    padding: 10px 20px;
    border-bottom: 1px solid #DCDFE6;
}

.content-row {
    overflow: hidden;
}

/* 分页 */
.pagination {
    width: 100%;
    padding: 20px;
    border-top: 1px solid #DCDFE6;
}
</style>
